package tree.easy;

import tree.TreeNode;

/**
 * 112. 路径总和
 */
public class HasPathSum {
    public static void main(String[] args) {
        HasPathSum hasPathSum = new HasPathSum();
        System.out.println(hasPathSum.hasPathSum(TreeNode.ROOT, 6));
    }

    /**
     * 深度优先
     * @param root
     * @param sum
     * @return
     */
    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) {
            return false;
        }
        if (root.left == null && root.right == null) {
            return sum == root.val;
        }
        return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);

    }

}
